package eclipse.lib.sockets;

import java.net.*;
import java.io.*;
/**
 * Class that represents a Client side Socket object.
 */
public class ClientSocket
{
	private Socket			client;
	private int 			port = 4000;
	private String 			host = "127.0.0.1";
	private BufferedReader 	input;
	private BufferedWriter 	output;
	/** Creates a new Socket and connects using port 4000 to local host */
	public ClientSocket()
	{		
		connect(port, host);
	}
	/**
	 * Creates a new Socket using a specified port of the local host.
	 * @param iPort Port to connect to.
	 */
	public ClientSocket(int iPort)
	{
		setPort(iPort);
		connect(port, host);
	}
	/**
	 * Creates a new Socket using a specified IP of the host using port 4000
	 * @param sHost IP to connect to.
	 */
	public ClientSocket(String sHost)
	{
		setHost(sHost);
		connect(port, host);
	}
	/** 
	 * Creates a new Socket using a specified Port and IP.
	 * @param iPort Port of the Server
	 * @param sHost IP of the Server
	 */
	public ClientSocket(int iPort, String sHost)
	{
		setPort(iPort);
		setHost(sHost);
		connect(port, host);
	}	
	/**
	 * Creates a new Socket using a specified Port and IP.
	 * @param sHost Port of the Server
	 * @param iPort IP of the Server
	 */
	public ClientSocket(String sHost, int iPort)
	{
		setPort(iPort);
		setHost(sHost);
		connect(port, host);		
	}
	/**
	 * Creates a new Socket from an existing Socket.
	 */
	public ClientSocket(Socket sckClient)
	{
		try
		{
			client = sckClient;
			input 	= new BufferedReader
			(
				new InputStreamReader
				(
						client.getInputStream()
				)
			);
			output = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));	        
		}
		catch(Exception e)
		{
			eclipse.ErrorManager.handleError(e);
		}
	}
	/**
	 * Connects to the Server
	 * @param sHost IP of the Server
	 * @param iPort Port of the Server
	 */
	public void connect(String sHost, int iPort)
	{
		connect(iPort, sHost);
	}
	/**
	 * Connects to the Server
	 * @param iPort Port of the Server
	 * @param sHost IP of the Server
	 */
	public void connect(int iPort, String sHost)
	{
		System.out.println(sHost + ":" + iPort);
		try
		{
			client 	= new Socket(host, port);
			input 	= new BufferedReader			
			(
				new InputStreamReader
				(
						client.getInputStream()
				)
			);
			output = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));				
		}
		catch(Exception e)
		{
			eclipse.ErrorManager.handleError(e);
		}
	}
	/** Forces reconnect to the Server */
	public void reConnect()
	{
		close();
		client = null;
		connect(port, host);
	}	
	/** Returns a String net message recieved from the server. */
	public String getString()
	{
		try
		{
			if(isConnected())					
				return input.readLine();			
			else
				return null;
		}
		catch(Exception e)
		{		
			close();
			client = null;
			return null;
		}
	}
	/** Returns an array of bytes recieved from the server. */
	public byte[] getBytes()
	{				
		byte[] comCode;
		byte[] retFail = {0};
		try
		{
				comCode = new byte[16];
				client.getInputStream().read(comCode, 0, 16);
				return comCode;			
		}
		catch(Exception e)
		{
			eclipse.ErrorManager.handleError(e);			
			return retFail;
		}		
	}
	/**
	 * Sends a message to the Server
	 * @param bInput Array of bytes to send
	 */
	public void sendMsg(byte[] bInput)
	{		
		try
		{
			client.getOutputStream().write(bInput, 0, 16);
		}
		catch(Exception e)
		{
			eclipse.ErrorManager.handleError(e);
		}
	}
	/**
	 * Sends a message to the Server
	 * @param sInput String to send
	 */
	public void sendMsg(String sInput)
	{
		try
		{
			output.write(sInput);
			output.flush();
		}
		catch(Exception e)
		{
			eclipse.ErrorManager.handleError(e);
		}
	}
	/** Returns if the Socket is connected to the Server */
	public boolean isConnected()
	{
		try
		{			
			if(client.isConnected())					
				return true;
			else
				return false;
		}
		catch(Exception e)
		{
			close();
			return false;
		}
	}
	/** Closes all Socket connection */
	public void close()
	{
		try 
		{
			client.close();
		}
		catch (Exception e)
		{	
			eclipse.ErrorManager.handleError(e);
		}
	}
	/** Returns the host IP. */
	public String getHost()
	{
		return host;
	}
	/** Returns the Port of the */
	public int getPort()
	{
		return port;
	}
	/** Sets the Host's IP. */
	public void setHost(String sHost)
	{
		host = sHost;
	}
	/** Sets the Host's Port. */
	public void setPort(int iPort)
	{
		port = iPort;
	}
	
}
